<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <FilterTable
      ref="table"
      height="auto"
      :columns="columns"
      :dataSource="dataSource"
      :onRowSelectChange="onRowSelectChange"
      :onColumnsChange="columns => this.columns = columns"
    >
    </FilterTable>
    <div style="width:100%;margin:10px 0;text-align: center;">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="add">确定</el-button>
        <el-button size="small" type="" icon="el-icon-plus" @click="cancle">取消</el-button>
    </div>
  </div>
</template>

<script>
  import { mapState } from 'vuex';
  import { dictionary } from '@/utils/dictMixin';
  import EmployeeAuthPanel from './employeeAuthPanel'
  import { orgPosition,positionData } from '@/api/common/auth/employeeAuth'
  
  export default {
    name: 'EmployeeAuth',
    mixins: [dictionary],
    data() {
      this.BaseTable = null;
      return {
        topFilterList: this.createTopFilterList(),
        columns: this.createTableColumns(),
        dataSource:[],
        params:{
            limit:5,
            pageNum:1,
            currentPage: 1,
            pageSize: 15,
            positionCode:'',
            positionName:'',
            dataSource:'10451001,10451003',
            dataType:10461001,
        },
        addData:''
      };
    },
    components: {
      EmployeeAuthPanel,
    },
    mounted () {
      this.getOrgPosition()  
      this.BaseTable = this.$refs.table.$refs.pageTable;
    },
    methods: {
      createTableColumns() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 70,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '职位代码',
            dataIndex: 'positionCode',
            width: 150,
            sorter: true,
            filter: true,
            filterType: 'input',
            // align:"center"
          },
          {
            title: '职位名称',
            dataIndex: 'positionName',
            sorter: true,
            filter: true,
            filterType: 'input',
            // align:"center"
          },
          {
            title: '数据来源',
            dataIndex: 'dataSource',
            sorter: true,
            filter: true,
            filterType: 'checkbox',
            filterItems: this.createDictList('1045'),
            /*tc-dode报错 
            render: props => {
              return <el-tag type={props.row.dataSources === 10041001 ? 'success' : 'primary'} close-transition>{this.createDictText(props.row.dataSources, '1045')}</el-tag>;
            } */
          },
        ];
      },
      createTopFilterList() {
        return [
          {
            type: 'INPUT',
            label: '职位代码',
            fieldName: 'positionCode',
            placeholder: '员工编号...'
          },
          {
            type: 'INPUT',
            label: '职位名称',
            fieldName: 'positionName',
            placeholder: '账号...'
          }
        ];
      },
    //   初始化
      getOrgPosition(){
          orgPosition(this.params).then(res=>{
            this.dataSource=res.data.records
          }).catch(err=>{})
      },
     //搜索
      changeHandle(val) {
        //   console.log(val,'val')
          this.params.positionName=val.positionName
          this.params.positionCode=val.positionCode
          orgPosition(this.params).then(res=>{
              this.dataSource=res.data.records
          }).catch(err=>{})
      },
      //重置
      collapseHandle() {
        this.$nextTick(() => {
          this.BaseTable.EXECUTE_RESET_HEIGHT();
        });
      },
        //选择数据行
      onRowSelectChange(data){
          this.addData=data
      },
    //   确定
    add(){
        if(!this.addData){
            this.$message({
            message: '请选择一条数据',
            type: 'info'
            });
        }else{
            console.log(this.addData,"data")
            let positionId=[]
            this.addData.forEach(x => {
                positionId.push(x.orgPositionId)
            });
            positionData(positionId).then(res=>{
              console.log(res,"positionData")
              if(res.resultCode == 200){
                  // this.$message({
                  //   message: `保存成功`,
                  //   type: 'success'
                  // });

                  this.cancle(res.data.treejson)
              }else{
                this.$message({
                  message: `${res.errMsg}`,
                  type: 'info'
                });
              }
              
            }).catch(err=>{})
            // this.cancle()
        }
        
    },
    // 取消
    cancle(dataTree){
        this.$emit("cancleHandle",false,dataTree)
    }
    },
  };
</script>

<style lang="less" scoped>

</style>
